package com.hliushi.demo.controller;

import com.hliushi.demo.entity.CityDictionary;
import com.hliushi.demo.service.CityDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * @author llhuang10
 * @date 2021/7/21 10:48
 */
@RestController
@RequestMapping("/api")
public class DictionaryController {


    @Autowired
    private CityDictionaryService dictionaryService;

    @Value("${bt.info}")
    private String info;

    /**
     * 获取信息
     *
     * @param
     * @return
     */
    @GetMapping("/info")
    public ResponseEntity<String> printInfo() {
        return ResponseEntity.ok(info);
    }

    /**
     * 创建字典
     *
     * @param cityDictionary 字典
     * @return
     */
    @PostMapping("/dictionaries")
    public ResponseEntity<CityDictionary> createDirt(@RequestBody CityDictionary cityDictionary) throws URISyntaxException {
        return ResponseEntity.created(new URI("/dictionaries")).body(dictionaryService.addDict(cityDictionary));
    }


    /**
     * 更新字典
     *
     * @param dictionary 字典
     * @return
     */
    @PutMapping("/dictionaries")
    public ResponseEntity<CityDictionary> updateDict(@RequestBody CityDictionary dictionary) {
        CityDictionary result = dictionaryService.update(dictionary);
        return ResponseEntity.ok(result);
    }


    /**
     * @param
     * @return
     */
    @DeleteMapping("/dictionaries/{dictId}")
    public void deleteDict(@PathVariable String dictId) {
        dictionaryService.delete(dictId);

    }


    /**
     * @param
     * @return
     */
    @GetMapping("/dictionaries")
    public ResponseEntity<List<CityDictionary>> getDictionaries() {
        List<CityDictionary> result = dictionaryService.getList();

        return ResponseEntity.ok(result);
    }


    /**
     * @param
     * @return
     */
    @GetMapping("/dictionaries/page")
    public ResponseEntity<Page<CityDictionary>> getDictionaries(Pageable pageable) {
        Page<CityDictionary> result = dictionaryService.getListPage(pageable);
        return ResponseEntity.ok(result);
    }

}
